package com.onstar.cn.common.log;

import android.content.Context;
import android.util.Log;

import com.onstar.cn.common.IServerUrls;
import com.onstar.cn.common.util.CommonUtil;

/**
 * Log 日志工具类<br/>
 * 调用实例：LogManager.getInstance(Context).d or e or i or w(message);
 *
 * @author 7828wc
 */
public class LogManager {
    private static LogManager logger;
    private boolean isDebug = false;
    private static String TAG = "com.onstar.cn";

    public static LogManager getInstance(Context context) {
        if (logger == null) {
            if (context != null) {
                logger = new LogManager(context);
            } else {
                logger = new LogManager();
            }
        }
        return logger;
    }

    public static LogManager getInstance() {
        if (logger == null) {
            logger = new LogManager();
        }
        return logger;
    }

    private LogManager() {
        try {
            isDebug = Boolean.valueOf(IServerUrls.isDevelopMode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        TAG = "com.onstar.cn";
    }

    private LogManager(Context context) {
        try {
            isDebug = Boolean.valueOf(IServerUrls.isDevelopMode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        TAG = context.getApplicationContext().getPackageName();
    }

    public void d(String printMsg) {
        if (isDebug) {
            if (!CommonUtil.isNull(printMsg))
                Log.d(TAG, printMsg);
        }
    }

    public void i(String printMsg) {
        if (isDebug) {
            if (!CommonUtil.isNull(printMsg))
                Log.i(TAG, printMsg);
        }
    }

    public void w(String printMsg) {
        if (isDebug) {
            if (!CommonUtil.isNull(printMsg))
                Log.w(TAG, printMsg);
        }
    }

    public void e(String printMsg) {
        if (isDebug) {
            if (!CommonUtil.isNull(printMsg))
                Log.e(TAG, printMsg);
        }
    }

    public void e(String printMsg, Throwable tr) {
        if (isDebug) {
            if (!CommonUtil.isNull(printMsg))
                Log.e(TAG, printMsg, tr);
        }
    }

    public void e(Throwable tr) {
        e(tr.getMessage(), tr);
    }
}
